package com.example.wentong;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import java.text.SimpleDateFormat;

import androidx.annotation.NonNull;
import product.idcard.android.IDCardAPI;

/**
 * Created By kzz on 2021/7/22
 * Describe:
 */
public class KTWenTongAPI {

    private Context mContext;
    public static int InitSucess = -1;//初始化返回值
    public static boolean isExitApp = false;//是否退出APP
    public static boolean isFirstOnCreate = true;//是否是第一次创建
    private CopyAssetsUtil copyAssetsUtil;
    private IDCardAPI idCardAPI;
    private static KTWenTongAPI instance;
    private ReadDataListener readDataListener;
//    OcrThread ocrThread;
//    private  String strPath = Environment.getExternalStorageDirectory().toString();
//    public  String    strImagePath ="";
//    public  String    strImagePathIR = "";//红外图片路径
//    public  String    strImagePathUV = "";//紫外图片路径
//    public  String    strHeadImagePath = "";
//    public  String    strHeadImagePathEC = "";
//    public  String    strSIDHeadPath = "";
//    public  String    strSIDHeadPathName = "";
//    public  String    strTestPath = "";

    private KTWenTongAPI() {

    }

    public static KTWenTongAPI getInstance(Context mContext) {
        if (instance == null) {
            synchronized (KTWenTongAPI.class) {
                if (instance == null)
                    instance = new KTWenTongAPI(mContext);
            }
        }
        return instance;
    }

    private KTWenTongAPI(Context mContext) {
        this.mContext = mContext;
        isFirstOnCreate = true;
        //修改/dev/bus/usb/文件夹的权限，注意添加\n
        String commend = "chmod -R 777 /dev/bus/usb/  \n";
        if (RootCmd.isRoot()) {
            String result = RootCmd.execRootCmd(commend);
            result = RootCmd.execRootCmd(commend);
        }
        idCardAPI = new IDCardAPI();
        //实例化拷贝资源类
        copyAssetsUtil = new CopyAssetsUtil(mContext);
//        strTestPath = strPath + "/AndroidWTone/test.jpg";
//        strImagePath =strPath + "/AndroidWTone/test_Image.jpg";
//        strImagePathIR = strPath + "/AndroidWTone/test_ImageIR.jpg";
//        strImagePathUV = strPath + "/AndroidWTone/test_ImageUV.jpg";
//        strHeadImagePath = strPath + "/AndroidWTone/test_ImageHead.jpg";
//        strHeadImagePathEC = strPath + "/AndroidWTone/test_ImageHeadEc.jpg";
//        strSIDHeadPath = strPath + "/AndroidWTone/IDCardProdTest/";
//        strSIDHeadPathName = strPath + "/AndroidWTone/IDCardProdTest/zp.bmp";
//        ocrThread = new OcrThread(isFirstOnCreate, handler, idCardAPI, copyAssetsUtil);
    }

    /**
     * 开启识别线程
     */
    private void open() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //开启OCR
                OcrThread ocrThread = new OcrThread(handler, idCardAPI, copyAssetsUtil);
                ocrThread.start();//end -> run
            }
        }, 200);

    }

    public void open(boolean isExitApp) {
        KTWenTongAPI.isExitApp = isExitApp;
        open();
    }

    public void close() {
//        isFirstOnCreate = true;
        KTWenTongAPI.isExitApp = true;
        idCardAPI.FreeIDCard();
    }

    //消息循环 //end -> onCreate
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (!isExitApp) {
                switch (msg.what) {
                    case 0:
//                        mdialog = ProgressDialog.show(mContext, "", "核心初始化中....");
//                        readDataListener.onReadData(msg.what, (InfoBean) msg.obj);
//                        break;
                    case 10:
//                        if (mdialog != null) {
//                            mdialog.dismiss();
//                            mdialog = null;
//                        }
                    case 15:
                        readDataListener.onReadData(msg.what, (InfoBean) msg.obj);
                        break;
                    case 20:
                    case 80:
                    case 100:
                    case 101:
                    case 102:
                    case 103:
                        readDataListener.onReadData(msg.what, (InfoBean) msg.obj);
                        open();
                        break;
                    default:
                        readDataListener.onReadData(msg.what, (InfoBean) msg.obj);
                        break;
                }
            }
            return false;
        }
    });

    public boolean isFirstOnCreate() {
        return isFirstOnCreate;
    }

    public void setIsFirstOnCreate(boolean isFirstOnCreate) {
        KTWenTongAPI.isFirstOnCreate = isFirstOnCreate;
    }

    public ReadDataListener getReadDataListener() {
        return readDataListener;
    }

    public void setReadDataListener(ReadDataListener readDataListener) {
        this.readDataListener = readDataListener;
    }
}
